package pers.cj.pdos.controller;


import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import pers.cj.pdos.controller.base.BaseController;
import pers.cj.pdos.controller.entity.ResponseData;
import pers.cj.pdos.controller.entity.ResponseEntity;
import pers.cj.pdos.entity.*;
import pers.cj.pdos.service.IBorrowInBackService;
import pers.cj.pdos.utils.DataUtil;
import pers.cj.pdos.utils.PdosUtils;
import pers.cj.pdos.utils.GoodsUtil;
import pers.cj.pdos.utils.Utils;
import pers.cj.pdos.vo.BorrowInBackGoodsVo;
import pers.cj.pdos.vo.BorrowInBackVo;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chenj
 * @since 2020-03-25
 */
@Controller
@RequestMapping("/house")
public class BorrowInBackController extends BaseController {
    @Autowired
    IBorrowInBackService borrowInBackService;

    @RequiresPermissions("borrowIn:insert")
    @RequestMapping("/borrowInBackNewByBorrowIn")
    public String borrowInBackNewByBorrowIn(Model model, String borrowInId){
        model.addAttribute("borrowInId", borrowInId);
        return "/borrowInBack/borrowInBackNewByBorrowIn";
    }

    @RequiresPermissions("borrowIn:query")
    @RequestMapping("/borrowInBackDetail")
    public String borrowInBackDetail(Model model, String id){
        model.addAttribute("id",id);
        return "/borrowInBack/borrowInBackDetail";
    }

    @ResponseBody
    @RequiresPermissions("borrowIn:insert")
    @RequestMapping("/borrowInBack/add")
    public ResponseEntity add(@RequestBody BorrowInBackVo borrowInBackVo){

        Company company = getCompany();
        BusinessSet businessSet = getBusinessSet();

        if (Utils.isEmpty(borrowInBackVo.getNo())){
            return ResponseEntity.fail("编号不能为空");
        }

        if (Utils.isEmpty(borrowInBackVo.getBorrowInBackGoodsVoList())){
            return ResponseEntity.fail("您未选择任何商品");
        }

        if (borrowInBackVo.getDate() == null){
            return ResponseEntity.fail("业务日期不能为空");
        }

        if (borrowInBackService.checkNo(borrowInBackVo.getNo(),company.getId())){
            return ResponseEntity.fail("编号已被使用");
        }

        if (getHouseLocking(borrowInBackVo.getHouseId())) {
            return ResponseEntity.fail("仓库被锁定，无法归还");
        }

        GoodsUtil goodsUtil = new GoodsUtil(borrowInBackVo.getHouseId(),borrowInBackVo.getBorrowInBackGoodsVoList(),null);
        if (!PdosUtils.houseInOut(businessSet)) {
            //未启用出入库管理，会直接进行出库操作，所以要进行库存的比对
            String msg = goodsUtil.checkStock();
            if (msg!=null){
                return ResponseEntity.fail(msg);
            }
        }

        //准备插入数据库的数据
        BorrowInBack borrowInBack = DataUtil.genEntityFromVo(BorrowInBack.class, borrowInBackVo);
        DataUtil.processEntity(company,borrowInBack);
        if (!PdosUtils.houseInOut(businessSet)) {
            borrowInBack.setStatus(1);
        }else{
            borrowInBack.setStatus(2);
        }
        borrowInBack.setMakeUserId(getLoginUserId());
        borrowInBack.setMakeTime(new Date());
        borrowInBack.setClose(false);

        List<BorrowInBackGoods> borrowInBackGoodsList = new ArrayList<>();
        List<GoodsSn> goodsSnList = new ArrayList<>();
        List<GoodsSnLog> goodsSnLogList = new ArrayList<>();

        for (BorrowInBackGoodsVo vo:borrowInBackVo.getBorrowInBackGoodsVoList()) {
            BorrowInBackGoods entity = DataUtil.genEntityFromVo(BorrowInBackGoods.class, vo);
            DataUtil.processEntity(company,entity);
            entity.setBorrowInBackId(borrowInBack.getId());
            borrowInBackGoodsList.add(entity);

            if (!Utils.isEmpty(vo.getGoodsSnList())){
                for (GoodsSn goodsSn : vo.getGoodsSnList()) {
                    goodsSnList.add(goodsSn);

                    GoodsSnLog log = new GoodsSnLog();
                    DataUtil.processEntity(company,log);
                    log.setGoodsId(goodsSn.getGoodsId());
                    log.setSn(goodsSn.getSn());
                    log.setType(13);
                    log.setBorrowInBackGoodsId(entity.getId());
                    goodsSnLogList.add(log);
                }
            }
        }

        borrowInBackService.insert(businessSet, borrowInBack, borrowInBackGoodsList,goodsSnList,goodsSnLogList);

        if (!Utils.isEmpty(goodsSnList) && PdosUtils.houseInOut(businessSet)) {
            //已启用待出库入管理，但是前台传入了sn号，提示
            return ResponseEntity.success("处理成功，管理员在您新增单据过程中启用了出入库管理，录入的sn需要在出库时重新选择");
        }
        return ResponseEntity.success();
    }

    @ResponseBody
    @RequestMapping("/borrowInBack/getVoById")
    public ResponseData getVoById(String id) {
        return ResponseData.success(borrowInBackService.getVoById(id));
    }

    @ResponseBody
    @RequestMapping("/borrowInBack/listVoByBorrowIn")
    public ResponseData listVoByBorrowIn(String borrowInId) {
        return ResponseData.success(borrowInBackService.listVoByBorrowInId(borrowInId));
    }
}
